package com.ohimy.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.Order;
import org.springframework.stereotype.Controller;

import com.ohimy.entity.InfoItem;
import com.ohimy.entity.InfoItemComment;
import com.ohimy.entity.InfoSort;
import com.ohimy.entity.InfoSubSort;
import com.ohimy.service.impl.InfoService;
import com.ohimy.service.impl.UserService;

@Controller("infoAction")
public class InfoAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	@Resource(name = "infoService")
	private InfoService infoService;

	@Resource(name = "userService")
	private UserService userService;

	private InfoSort infoSort = new InfoSort();
	private InfoSubSort infoSubSort = new InfoSubSort();
	private InfoItem infoItem = new InfoItem();
	private InfoItemComment infoItemComment = new InfoItemComment();
	private List<InfoSort> infoSortList;
	private List<InfoSubSort> infoSubSortList;
	private List<InfoItem> infoItemList;
	private List<InfoItemComment> infoCommentList;
	private String page_index, maxResult, sub; // 当前页，总数,二级目录ID
	private long total; // 总数, 二级目录ID
	private List<InfoItem> foodList, funList, shoppingList, trainingList;

	// private String ssid,did,

	@Override
	public String execute() throws Exception {
		foodList = infoService.getItemBySortId(1);
		funList = infoService.getItemBySortId(2);
		shoppingList = infoService.getItemBySortId(3);
		trainingList = infoService.getItemBySortId(4);
		result = "/WEB-INF/site/info/";
		return "ohimy";
	}

	/**
	 * 新添栏目
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addSort() throws Exception {
		infoSort.setCreated(new Date());
		infoService.insertInfoSort(infoSort);
		return null;
	}

	/**
	 * 更新栏目
	 * 
	 * @return
	 * @throws Exception
	 */
	public String updateSort() throws Exception {
		infoSort.setId(id);
		infoService.updateInfoSort(infoSort);
		return null;
	}

	/**
	 * 删除栏目
	 * 
	 * @return
	 * @throws Exception
	 */
	public String deleteSort() throws Exception {
		infoService.deleteInfoSort(id);
		return null;
	}

	// //////////////////////////

	/**
	 * 根据一级目录ID查看其二级目录
	 * 
	 * @return
	 * @throws Exception
	 */
	public String showSubSort() throws Exception {
		infoSort = infoService.getInfoSort(id);
		infoSubSortList = infoService.getAllInfoSubSort(id);
		result = "/WEB-INF/site/ajax/subSort_list.jsp";
		return "ohimy";
	}

	/**
	 * ajax根据一级目录ID得到其二级目录
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getSubSortSelect() throws Exception {
		infoSubSortList = infoService.getAllInfoSubSort(id);
		result = "/WEB-INF/site/ajax/info_select_subSort.jsp";
		return "ohimy";
	}

	/**
	 * 新添二级栏目
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addSubSort() throws Exception {
		infoSubSort.setInfoSort(infoSort);
		infoSubSort.setCreated(new Date());
		infoService.insertInfoSubSort(infoSubSort);
		return null;
	}

	/**
	 * 更新二级栏目
	 * 
	 * @return
	 * @throws Exception
	 */
	public String updateSubSort() throws Exception {
		infoSubSort.setId(id);
		infoSubSort.setInfoSort(infoSort);
		infoService.updateInfoSubSort(infoSubSort);
		return null;
	}

	/**
	 * 删除二级栏目
	 * 
	 * @return
	 * @throws Exception
	 */
	public String deleteSubSort() throws Exception {
		System.out.println(id);
		infoService.deleteInfoSubSort(id);
		return null;
	}

	// ///////////////////////////////
	/**
	 * 显示所有的资讯列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String infoShow() throws Exception {
		int page_index = Integer.parseInt(this.page_index);
		int maxResult = Integer.parseInt(this.maxResult);
		infoItemList = infoService.getAllInfoItemByPager(
				page_index * maxResult, maxResult);
		result = "/WEB-INF/site/ajax/info_list.jsp";
		return "ohimy";
	}

	/**
	 * 查看资讯
	 * 
	 * @return
	 * @throws Exception
	 */
	public String seeInfo() throws Exception {
		infoItem = infoService.getInfoItem(id);
		result = "/WEB-INF/site/ajax/news_show.jsp";
		return "ohimy";
	}

	/**
	 * 用户查看资讯
	 * 
	 * @return
	 * @throws Exception
	 */
	public String show() throws Exception {
		// 通过id得到该资讯
		infoItem = infoService.getInfoItem(id);
		// 评论总数
		total = infoService.getInfoCommentCount(id);
		result = "/WEB-INF/site/template/info_eat.jsp";
		return "ohimy";
	}

	/**
	 * 新添资讯
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addInfo() throws Exception {
		// 当时间项未选择时，即为当前时间
		if (null == infoItem.getCreated() || "".equals(infoItem.getCreated()))
			infoItem.setCreated(new Date());
		infoItem.setCheck(1);
		infoItem.setInfoSubSort((infoService
				.getInfoSubSort(infoSubSort.getId())));
		infoService.insertInfoItem(infoItem);
		return null;
	}

	/**
	 * 更新资讯
	 * 
	 * @return
	 * @throws Exception
	 */
	public String updateInfo() throws Exception {
		infoItem.setId(id);
		infoService.updateInfoItem(infoItem);
		return null;
	}

	/**
	 * 查找资讯以更新
	 * 
	 * @return
	 * @throws Exception
	 */
	public String gotoInfo() throws Exception {
		infoItem = infoService.getInfoItem(id);
		infoSortList = infoService.getAllInfoSort();
		infoSubSortList=infoService.getAllInfoSubSort(infoItem.getInfoSubSort().getInfoSort().getId());
		result = "/WEB-INF/site/ajax/info_merge.jsp";
		return "ohimy";
	}

	/**
	 * 删除资讯
	 * 
	 * @return
	 * @throws Exception
	 */
	public String deleteInfo() throws Exception {
		infoService.deleteInfoItem(id);
		return null;
	}

	// ///////////////////////////////
	/**
	 * 显示所有的资讯评论列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String infoCommentShow() throws Exception {
		int page_index = Integer.parseInt(this.page_index);
		int maxResult = Integer.parseInt(this.maxResult);
		infoCommentList = infoService.getAllInfoCommentByPager(id, page_index
				* maxResult, maxResult);
		result = "/WEB-INF/site/ajax/info_reply.jsp";
		return "ohimy";
	}

	/**
	 * 查看资讯评论....
	 * 
	 * @return
	 * @throws Exception
	 */
	public String seeInfoComment() throws Exception {
		infoItemComment = infoService.getInfoComment(id);
		result = "/WEB-INF/site/ajax/news_show.jsp";
		return "ohimy";
	}

	/**
	 * 新添资讯评论
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addInfoComment() throws Exception {
		infoItemComment.setCreated(new Date());
		infoItemComment.setCheck(0);
		infoItemComment.setReplier(userService.findUserById(getSession()
				.getId()));
		infoItemComment.setInfoItem((infoService.getInfoItem(id)));
		infoService.insertInfoComment(infoItemComment);
		System.out.println(infoItemComment.getService());
		return null;
	}

	/**
	 * 更新资讯评论
	 * 
	 * @return
	 * @throws Exception
	 */
	public String updateInfoIntemComment() throws Exception {
		infoItem.setId(id);
		infoService.updateInfoItem(infoItem);
		return null;
	}

	/**
	 * 查找资讯评论以更新
	 * 
	 * @return
	 * @throws Exception
	 */
	public String gotoInfoComment() throws Exception {
		infoItem = infoService.getInfoItem(id);
		infoSortList = infoService.getAllInfoSort();
		result = "/WEB-INF/site/ajax/info_merge.jsp";
		return "ohimy";
	}

	/**
	 * 删除资讯评论
	 * 
	 * @return
	 * @throws Exception
	 */
	public String deleteInfoComment() throws Exception {
		infoService.deleteInfoItem(id);
		return null;
	}

	// ////////////////////////////////
	/**
	 * 用户查看制定分类
	 * 
	 * @return
	 * @throws Exception
	 */
	public String sort() throws Exception {
		// 通过id得到该资讯二级目录
		infoSubSortList = infoService.getAllInfoSubSort(id);
		// 得到下属所有资讯数量
		total = infoService.getItemBySortId(id).size();
		if (null == sub)
			sub = "0";
		// 得到该分类
		infoSort = infoService.getInfoSort(id);
		result = "/WEB-INF/site/template/sort_" + infoSort.getTemplate()
				+ ".jsp";
		return "ohimy";
	}

	/**
	 * 模糊查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String search() throws Exception {
		// 得到下属所有资讯数量
		long sub = Long.parseLong(this.sub);
		total = infoService.findInfoByPagerCount(id, sub, infoItem);
		// 通过id得到该资讯
		int page_index = Integer.parseInt(this.page_index);
		int maxResult = Integer.parseInt(this.maxResult);
		// 设置审核
		infoItem.setCheck(1);
		infoItemList = infoService.findInfoByPager(id, sub, infoItem,
				page_index * maxResult, maxResult, Order.asc("id"));
		result = "/WEB-INF/site/ajax/info_sort_show.jsp";
		return "ohimy";
	}

	/* ************************************** */
	public String getPage_index() {
		return page_index;
	}

	public void setPage_index(String page_index) {
		this.page_index = page_index;
	}

	public String getMaxResult() {
		return maxResult;
	}

	public void setMaxResult(String maxResult) {
		this.maxResult = maxResult;
	}

	public void setInfoService(InfoService infoService) {
		this.infoService = infoService;
	}

	public InfoSort getInfoSort() {
		return infoSort;
	}

	public void setInfoSort(InfoSort infoSort) {
		this.infoSort = infoSort;
	}

	public InfoSubSort getInfoSubSort() {
		return infoSubSort;
	}

	public void setInfoSubSort(InfoSubSort infoSubSort) {
		this.infoSubSort = infoSubSort;
	}

	public InfoItem getInfoItem() {
		return infoItem;
	}

	public void setInfoItem(InfoItem infoItem) {
		this.infoItem = infoItem;
	}

	public List<InfoSort> getInfoSortList() {
		return infoSortList;
	}

	public void setInfoSortList(List<InfoSort> infoSortList) {
		this.infoSortList = infoSortList;
	}

	public List<InfoSubSort> getInfoSubSortList() {
		return infoSubSortList;
	}

	public void setInfoSubSortList(List<InfoSubSort> infoSubSortList) {
		this.infoSubSortList = infoSubSortList;
	}

	public List<InfoItem> getInfoItemList() {
		return infoItemList;
	}

	public void setInfoItemList(List<InfoItem> infoItemList) {
		this.infoItemList = infoItemList;
	}

	public InfoItemComment getInfoItemComment() {
		return infoItemComment;
	}

	public void setInfoItemComment(InfoItemComment infoItemComment) {
		this.infoItemComment = infoItemComment;
	}

	public List<InfoItemComment> getInfoCommentList() {
		return infoCommentList;
	}

	public void setInfoCommentList(List<InfoItemComment> infoCommentList) {
		this.infoCommentList = infoCommentList;
	}

	public InfoService getInfoService() {
		return infoService;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getSub() {
		return sub;
	}

	public void setSub(String sub) {
		this.sub = sub;
	}

	public List<InfoItem> getFoodList() {
		return foodList;
	}

	public void setFoodList(List<InfoItem> foodList) {
		this.foodList = foodList;
	}

	public List<InfoItem> getFunList() {
		return funList;
	}

	public void setFunList(List<InfoItem> funList) {
		this.funList = funList;
	}

	public List<InfoItem> getShoppingList() {
		return shoppingList;
	}

	public void setShoppingList(List<InfoItem> shoppingList) {
		this.shoppingList = shoppingList;
	}

	public List<InfoItem> getTrainingList() {
		return trainingList;
	}

	public void setTrainingList(List<InfoItem> trainingList) {
		this.trainingList = trainingList;
	}

}